package com.copa.l1418;

import java.util.*;

/**
 * @author copa
 * @createDate 2022-07-12 10:30
 * @function hashMap+hashSet
 */
public class Test1 {

    public List<List<String>> displayTable(List<List<String>> orders) {
        List<List<String>> table = new ArrayList<>();
        if (null == orders || orders.size() == 0) {
            return table;
        }
        Set<Integer> tableNoSet = new HashSet<>();
        Set<String> foodSet = new HashSet<>();
        HashMap<Integer, HashMap<String, Integer>> foodListMap = new HashMap<>();
        for (List<String> order : orders) {
            Integer tableNo = Integer.valueOf(order.get(1));
            String food = order.get(2);
            tableNoSet.add(tableNo);
            foodSet.add(order.get(2));
            HashMap<String, Integer> foodListByTableNo = foodListMap.getOrDefault(tableNo, new HashMap<>());
            foodListByTableNo.put(food, foodListByTableNo.getOrDefault(food, 0) + 1);
            foodListMap.put(tableNo, foodListByTableNo);
        }

        List<Integer> tableNoList = new ArrayList<>();
        for (Integer tableNo : tableNoSet) {
            tableNoList.add(tableNo);
        }
        Collections.sort(tableNoList);
        List<String> foodList = new ArrayList<>();
        for (String food : foodSet) {
            foodList.add(food);
        }
        Collections.sort(foodList);

        // 头部
        List<String> header = new ArrayList<>();
        header.add("Table");
        for (String food : foodList) {
            header.add(food);
        }
        table.add(header);

        // 菜单详情列表
        for (Integer tableNo : tableNoList) {
            List<String> content = new ArrayList<>();
            content.add(String.valueOf(tableNo));
            HashMap<String, Integer> foodMapByTableNo = foodListMap.get(tableNo);
            for (String food : foodList) {
                if (Objects.isNull(foodMapByTableNo.get(food))) {
                    content.add("0");
                    continue;
                }
                content.add(String.valueOf(foodMapByTableNo.get(food)));
            }
            table.add(content);
        }
        return table;
    }

    public static void main(String[] args) {
        List<List<String>> orders = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        tempList.add("David");tempList.add("3");tempList.add("Ceviche");
        orders.add(tempList);
        tempList = new ArrayList<>();
        tempList.add("Corina");tempList.add("10");tempList.add("Beef Burrito");
        orders.add(tempList);
        tempList = new ArrayList<>();
        tempList.add("David");tempList.add("3");tempList.add("Fried Chicken");
        orders.add(tempList);
        tempList = new ArrayList<>();
        tempList.add("Carla");tempList.add("5");tempList.add("Water");
        orders.add(tempList);
        tempList = new ArrayList<>();
        tempList.add("Carla");tempList.add("5");tempList.add("Ceviche");
        orders.add(tempList);
        tempList = new ArrayList<>();
        tempList.add("Rous");tempList.add("3");tempList.add("Ceviche");
        orders.add(tempList);
        List<List<String>> result = new Test1().displayTable(orders);
        for (List<String> list : result) {
            System.out.println(Arrays.toString(list.toArray(new String[5])));
        }

    }
}
